﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.IO;

namespace TinyERP.Framework.Config
{
    public class XmlConfigurationProvider : IApplicationConfigProvider
    {
        private ApplicationConfiguration configuration;
        private string filePath;
        private Assembly assembly;
        private string resourcePath;
        public XmlConfigurationProvider(string filePath)
        {
            this.filePath = filePath;
        }
        public XmlConfigurationProvider(Assembly assembly, string resourcePath)
        {
            this.assembly = assembly;
            this.resourcePath = resourcePath;
        }

        private void LoadConfigFromExternalFile(string filePath)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(filePath);
            LoadConfigurationFromXmlDocument(xmlDocument);
        }

        private void LoadConfigFromResourceFile(Assembly assembly, string resourcePath)
        {
            Stream stream = assembly.GetManifestResourceStream(resourcePath);
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(stream);
            LoadConfigurationFromXmlDocument(xmlDocument);
        }
        private void LoadConfigurationFromXmlDocument(XmlDocument xmlDocument)
        {
            LoadApplicationSetting(xmlDocument);
            LoadModuleList(xmlDocument);
            LoadUISetting(xmlDocument);
            LoadShortcutSetting(xmlDocument);
        }
        private void LoadModuleList(XmlDocument xmlDocument)
        {
            XmlNodeList xmlModuleNodeList = xmlDocument.SelectNodes("application-config/plugin-modules/module");
            foreach (XmlNode xmlModuleNode in xmlModuleNodeList)
            {
                ModuleElement module = new ModuleElement();
                module.Name = GetAttributeValue(xmlModuleNode, "name");
                module.UIAssembly = GetAttributeValue(xmlModuleNode, "assembly");
                module.BizAssembly = GetAttributeValue(xmlModuleNode, "bizAssembly");
                configuration.Modules.Add(module);
            }
        }
        private void LoadApplicationSetting(XmlDocument xmlDocument)
        {
            configuration.ApplicationName = xmlDocument.SelectSingleNode("application-config/app-name").InnerText;
            configuration.PluginPath = xmlDocument.SelectSingleNode("application-config/plugin-path").InnerText;
            configuration.ApplicationMode = xmlDocument.SelectSingleNode("application-config/app-mode").InnerText;
            configuration.AutoUpdateURL = xmlDocument.SelectSingleNode("application-config/auto-update-url").InnerText;

        }

        private void LoadShortcutSetting(XmlDocument xmlDocument)
        {
            configuration.CloseShortcut = xmlDocument.SelectSingleNode("application-config/shortcuts/close-shortcut").InnerText;
            configuration.EditShortcut = xmlDocument.SelectSingleNode("application-config/shortcuts/edit-shortcut").InnerText;
            configuration.FindShortcut = xmlDocument.SelectSingleNode("application-config/shortcuts/find-shortcut").InnerText;
            configuration.NewShortcut = xmlDocument.SelectSingleNode("application-config/shortcuts/new-shortcut").InnerText;
            configuration.DeleteShortcut = xmlDocument.SelectSingleNode("application-config/shortcuts/delete-shortcut").InnerText;
        }

        private void LoadUISetting(XmlDocument xmlDocument)
        {
            configuration.NavigationView = xmlDocument.SelectSingleNode("application-config/ui-settings/navigation-view").InnerText;
            configuration.DefaultLanguage = xmlDocument.SelectSingleNode("application-config/ui-settings/default-language").InnerText;
            configuration.DefaultTheme = xmlDocument.SelectSingleNode("application-config/ui-settings/default-theme").InnerText;
        }
        

       


        private string GetAttributeValue(XmlNode xmlNode, string attributeName)
        {
            if (xmlNode == null || attributeName == null)
            {
                return null;
            }
            if (xmlNode.Attributes[attributeName] == null)
            {
                return null;
            }
            return xmlNode.Attributes[attributeName].Value;
        }

        public void LoadConfiguration(ApplicationConfiguration configuration)
        {
            this.configuration = configuration;
            if (assembly != null)
            {
                this.LoadConfigFromResourceFile(assembly, resourcePath);
            }
            else
            {
                this.LoadConfigFromExternalFile(filePath);
            }
        }

        public void SaveConfiguration(ApplicationConfiguration configuration)
        {

        }
    }
}
